/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package presentation.proportional;

import control.CComponiMessaggio;
import customGuiControl.ImagePanel;
import customGuiControl.RoundedPanel2;
import entity.xml.EMessaggio;
import entity.xml.EUtente;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;

/**
 *
 * @author Quirino
 */
public class PComponiMessaggio implements MouseListener{
    
    private static PComponiMessaggio instance= null;
    private JPanel panel;
    private CComponiMessaggio cCompMess;
    private ArrayList<EMessaggio> messaggi;
    private ArrayList<EUtente> destinatari;
    private RoundedPanel2 menuSotto;
    private String contenutoMessaggio= "";
    private JTextArea txtAreaMessaggio= null;
    
    private PComponiMessaggio(){
        
        this.cCompMess= CComponiMessaggio.getInstance();
        
    }
     public RoundedPanel2 getMenuSotto(gui instance){
         if(this.menuSotto==null)refreshMenuSotto(instance);
         return this.menuSotto;
     }
    
    public void refreshMenuSotto(gui instance){
        
        /*roundedPanel rp= new roundedPanel(20, new Color(255,255,255),
                                                            new Color(229,229,229),
                                                            2f, Color.BLACK,
                                                            12114.286f,100f,Color.BLACK,
                                                            0.8f,"menuSotto");*/
        RoundedPanel2 rp=new RoundedPanel2();
        rp.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        
        gbc.gridx=0;
        gbc.gridy=0;
        gbc.weightx=0.49;
        gbc.weighty=1.0;
        gbc.fill=GridBagConstraints.BOTH;
        Utility.addInviaButton(gbc, rp, this, instance);
        
        
        gbc.gridx=1;
        gbc.gridy=0;
        gbc.weightx=0.01;
        gbc.weighty=1.0;
        gbc.fill=GridBagConstraints.BOTH;
        Utility.addPaddingButton(gbc, rp);
        
        gbc.gridx=2;
        gbc.gridy=0;
        gbc.weightx=0.49;
        gbc.weighty=1.0;
        gbc.fill=GridBagConstraints.BOTH;
        Utility.addDestinatarioButton(gbc, rp, this, instance);
        
        gbc.gridx=3;
        gbc.gridy=0;
        gbc.weightx=0.01;
        gbc.weighty=1.0;
        gbc.fill=GridBagConstraints.BOTH;
        Utility.addPaddingButton(gbc, rp);
        
        gbc.gridx=4;
        gbc.gridy=0;
        gbc.weightx=0.49;
        gbc.weighty=1.0;
        gbc.fill=GridBagConstraints.BOTH;
        Utility.addFotoVideoButton(gbc, rp, this, instance);
        
        gbc.gridx=5;
        gbc.gridy=0;
        gbc.weightx=0.01;
        gbc.weighty=1.0;
        gbc.fill=GridBagConstraints.BOTH;
        Utility.addPaddingButton(gbc, rp);
        
        gbc.gridx=6;
        gbc.gridy=0;
        gbc.weightx=0.49;
        gbc.weighty=1.0;
        gbc.fill=GridBagConstraints.BOTH;
        Utility.addAnnullaButton(gbc, rp, this, instance);
        
        
        this.menuSotto= rp;
        
    }
    
    public static PComponiMessaggio getInstance(){
        
        if(instance==null) instance= new PComponiMessaggio();
        return instance;
        
    }
    
    public JPanel getPanel() {
        return panel;
    }

    public void setPanel(JPanel panel) {
        this.panel = panel;
    }
    
    public void setDestinatari(ArrayList<EUtente> destinatari){
        this.destinatari=destinatari;
    }
    public void setMessaggi(ArrayList<EMessaggio> m){
        this.messaggi=m;
    }
    public void refreshPanel(ArrayList<EUtente> destinatari){
        
        int widthPanelBacheca= gui.getIstance().getPanelBachecaContainer().getWidth();
        int heightPanelBacheca= gui.getIstance().getPanelBachecaContainer().getHeight();
        int w= Math.round((float) (widthPanelBacheca/2));
        int h= Math.round((float) (heightPanelBacheca/1.1));
        
        this.panel= new JPanel(new FlowLayout(FlowLayout.CENTER));
        this.panel.setOpaque(false);
        
        Dimension d = this.cCompMess.getDimensionFoglio(2, 1.1);
        Dimension dimBacheca= this.cCompMess.getDimensionBacheca();
        
        int left= ((int)dimBacheca.width/2)-((int)(d.width/2));
        int top= ((int)dimBacheca.height/2)-((int)(d.height/2));
        
        
        JLayeredPane jlp= new JLayeredPane();
        jlp.setPreferredSize(new Dimension((int)d.getWidth(), (int)d.getHeight()));
        jlp.setBackground(Color.red);
        jlp.setBounds(left, top-50, (int)d.getWidth(), (int)d.getHeight());
        jlp.setOpaque(false);
        
        ImageIcon imgIcon= Utility.getResizedImageIcon("foglio.png", (int)d.getWidth(), (int)d.getHeight()-10);
        JLabel foglioContainer= new JLabel(imgIcon);
        foglioContainer.setBounds(0, 0, (int)d.getWidth(), (int)d.getHeight()-10);
        jlp.add(foglioContainer, new Integer(0));
        
        JLabel labelDestinatari= new JLabel();
        Font fontHeaderFoglio= new Font("Droid Sans", Font.BOLD, (int)d.getWidth()/24);
        labelDestinatari.setFont(fontHeaderFoglio);
        String str= "<html><b><u>Messaggio verso:</u></b> ";
        for(int i=0; i<destinatari.size(); i++){
            
            if(i!=0) str+= ",  " ;
            str+= destinatari.get(i).getNickname() + " ";
            
        }
        str+="</html>";
        labelDestinatari.setText(str);
        labelDestinatari.setBounds((int)d.getWidth()/8, 0, (int)d.getWidth()-(int)d.getWidth()/10, (int)d.getHeight()/5);
        jlp.add(labelDestinatari, new Integer(1));
        
        Font fontContenutoFoglio= new Font("Droid Sans", Font.PLAIN, (int)d.getWidth()/30);
        this.txtAreaMessaggio= new JTextArea(this.contenutoMessaggio);
        this.txtAreaMessaggio.setBounds((int)d.getWidth()/8, (int)d.getHeight()/5, (int)d.getWidth()-(int)d.getWidth()/6, (int)d.getHeight()-(int)d.getHeight()/10);
        this.txtAreaMessaggio.setOpaque(false);
        this.txtAreaMessaggio.setEditable(true);
        this.txtAreaMessaggio.setFont(fontContenutoFoglio);
        this.txtAreaMessaggio.setLineWrap(true);
        this.txtAreaMessaggio.setWrapStyleWord(true);
        jlp.add(this.txtAreaMessaggio, new Integer(2));
        
        this.panel.add(jlp, JLayeredPane.PALETTE_LAYER);
        
        
    }

    public void mouseClicked(MouseEvent e) {
        
        String sourceClass= e.getSource().getClass().toString();
        
        if( sourceClass.contains("ImagePanel")){
            
            String pathImg= ((ImagePanel) e.getSource()).getPathImg();
            
            if(pathImg.contains("invia")){
                
                boolean flag= false;
                flag= this.cCompMess.sendMessage(PContattaPersona.getInstance().getReceiverList(), this.txtAreaMessaggio.getText());
                String resStr= "";
                if(flag) resStr= "Messaggio inviato correttamente.";
                else resStr= "Problemi con la spedizione del messaggio.";
                JOptionPane.showMessageDialog(null, resStr);
                
                gui.getIstance().buttonClicked(gui._vaiBachecaContatto);
            }
            
        }    
        
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }
    
}
